<?php

/**
 * Description of NumberValidator
 *
 * @author verminu
 */
class NumberValidator extends sfValidatorRegex
{
  const PATTERN = '/^[0-9]+[a-z]*$/i';
  const DB_EXISTS = 'db_exists';
  
  protected function configure($options = array(), $messages = array())
  {
    parent::configure($options, $messages);
    
    $this->addRequiredOption('scara');
    
    $this->setOption('pattern', self::PATTERN);
    $this->setOption('max_length',  6);
    
    $this->addMessage(self::DB_EXISTS, 'Numărul este deja înregistrat în baza de date.');
  }
   
  protected function doClean($value)
  {
    $nsv = new NoSpacesValidator();
    $value = $nsv->clean($value);
    
    $value = parent::doClean($value);
    $value = strtoupper($value);
    
    // verific daca nr. ap. exista in baza de date
    if (ApartamentPeer::numberExists($value, $this->getOption('scara')->getId()))
    {
      throw new sfValidatorError($this, self::DB_EXISTS);
    }
    
    return $value;
  }
}
